package com.labichaoka.chaoka.ui.record.borrow;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.labichaoka.chaoka.R;
import com.labichaoka.chaoka.base.BaseFragment;
import com.labichaoka.chaoka.entity.OrderListResponse;
import com.labichaoka.chaoka.ui.record.BorrowOrderAdapter;
import com.labichaoka.chaoka.ui.web.WebViewActivity;
import com.labichaoka.chaoka.utils.SharedPreferencesManager;
import com.labichaoka.chaoka.utils.ToastUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;


public class BorrowFragment extends BaseFragment implements BorrowView {


    @BindView(R.id.borrow_list)
    RecyclerView borrowList;
    @BindView(R.id.empty_layout)
    RelativeLayout emptyLayout;


    private BorrowPresenter presenter;

    @Override
    protected int initLayoutId() {
        return R.layout.fragment_borrow;
    }

    @Override
    protected void initView() {
        borrowList.setLayoutManager(new LinearLayoutManager(getActivity()));
    }

    @Override
    protected void initData() {
        presenter = new BorrowPresenterImpl(this, new BorrowInteractorImpl());
        presenter.getOrderList();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void showProgress() {
        showLoadingProgress();
    }

    @Override
    public void hideProgress() {
        hideLoadingPregress();
    }

    @Override
    public void showMessage(String str) {
        ToastUtils.show(str);
    }

    @Override
    public void setData(OrderListResponse response) {
        if (response.getData() == null) {
            ToastUtils.show("获取数据异常,请稍后重试!");
            return;
        }
        Gson gson = new Gson();
        String jsonStr = gson.toJson(response);
        SharedPreferencesManager.getInstance().putString("orderList", jsonStr);

        int count = response.getData().getOrderLoanMoneyList().size();
        if (count == 0) {
            emptyLayout.setVisibility(View.VISIBLE);
        }else{
            emptyLayout.setVisibility(View.GONE);
        }

        BorrowOrderAdapter adapter = new BorrowOrderAdapter(mContext, response.getData().getOrderLoanMoneyList());
        borrowList.setAdapter(adapter);
        adapter.setOnItemClickListener((adapter1, view, position) -> {
            String orderId=response.getData().getOrderLoanMoneyList().get(position).getOrderNo();
            Bundle bundle=new Bundle();
            bundle.putString("orderId",orderId);
            bundle.putString("type","3");
            gotoActivity(mContext,WebViewActivity.class,bundle);
        });


    }
}
